package com.thinkfit.jy.fragment;

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.common.lib.fragment.BaseFragment;
import com.common.lib.manager.DataManager;
import com.common.lib.mvp.contract.EmptyContract;
import com.common.lib.mvp.presenter.EmptyPresenter;
import com.thinkfit.jy.R;
import com.thinkfit.jy.activity.BrandInfoActivity;
import com.thinkfit.jy.activity.VersionActivity;
import com.thinkfit.jy.utils.DeviceTypeUtils;

/**
 * 关于界面
 */
public class AboutFragment extends BaseFragment<EmptyContract.Presenter> implements EmptyContract.View {

    @NonNull
    @Override
    protected EmptyContract.Presenter onCreatePresenter() {
        return new EmptyPresenter(this);
    }

    @Override
    protected int getLayoutId() {
        return DataManager.Companion.getInstance().getMainStyle() == 3 ? R.layout.fragment_about_xm : R.layout.fragment_about;
    }

    @Override
    protected void initView(@NonNull View view, @Nullable Bundle savedInstanceState) {
        setViewsOnClickListener(R.id.tvBack, R.id.tvBrandInfo, R.id.llAutoUpdate, R.id.llVersion);
        setImage(R.id.ivAutoUpdateSwitch, DataManager.Companion.getInstance().getIsAutoUpdate() ?
                R.drawable.app_switch_on : (DataManager.Companion.getInstance().getMainStyle() == 3 ? R.drawable.app_switch_off_xm : R.drawable.app_switch_off));
        // 获取包管理器对象
        PackageManager packageManager = getContext().getPackageManager();
        try {
            // 获取当前应用程序的包名
            String packageName = getContext().getPackageName();

            // 根据包名获取应用程序的版本信息
            PackageInfo packageInfo = packageManager.getPackageInfo(packageName, 0);

            // 输出版本号
            int versionCode = packageInfo.versionCode;
            String versionName = packageInfo.versionName;
            setText(R.id.tvVersion,versionName);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }


    }

    @Override
    public void onStop() {
        super.onStop();
        DeviceTypeUtils.showBackButton(getContext().getApplicationContext());
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tvBrandInfo:
                openActivity(BrandInfoActivity.class);
                break;
            case R.id.llVersion:
                openActivity(VersionActivity.class);
                break;
            case R.id.llAutoUpdate:
                if (DataManager.Companion.getInstance().getIsAutoUpdate()) {
                    DataManager.Companion.getInstance().saveIsAutoUpdate(false);
                    setImage(R.id.ivAutoUpdateSwitch,
                            (DataManager.Companion.getInstance().getMainStyle() == 3 ? R.drawable.app_switch_off_xm : R.drawable.app_switch_off));
                } else {
                    DataManager.Companion.getInstance().saveIsAutoUpdate(true);
                    setImage(R.id.ivAutoUpdateSwitch, R.drawable.app_switch_on);
                }
                break;
            case R.id.tvBack:
                getParentFragmentManager().popBackStack();
                break;
        }
    }
}
